package com.ldy.core.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.core.common.Result;
import com.ldy.core.entity.BaseAuditLog;
import com.ldy.core.service.BaseAuditLogService;

/**
 * 审计日志
 */
@RestController
@RequestMapping("/audit")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class AuditLogController {
	
	@Autowired
	private BaseAuditLogService auditLogService;

	/**
	 * 列表分页查询
	 * @param current
	 * @param size
	 * @param keyword
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, String keyword) {
		Page<BaseAuditLog> page = new Page<BaseAuditLog>(current, size);
		QueryWrapper<BaseAuditLog> query = new QueryWrapper<>();
		if (StringUtils.hasText(keyword)) {
			query.and(ct -> ct.like("username", keyword).or().like("client_ip", keyword).or().like("request_uri", keyword));
		}
		query.orderByDesc("create_time");
		return Result.success(auditLogService.page(page, query));
	}
	
	/**
	 * 列表查询
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList() {
		QueryWrapper<BaseAuditLog> query = new QueryWrapper<>();
		query.orderByDesc("create_time");
		return Result.success(auditLogService.list(query));
	}
}
